﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Trans.customer;
using Trans.city;
using Trans;

namespace Trans.order
{
    public partial class Add_order : Form
    {
        DataRowView row;
        public Add_order()
        {
            InitializeComponent();
        }

        private void Add_order_Load(object sender, EventArgs e)
        {
           
            row = (DataRowView)tborderBindingSource.AddNew();
        } 

        private void bt_sender_Click(object sender, EventArgs e)
        {
            Select_customer form = new Select_customer();

            if (form.ShowDialog() == DialogResult.OK)
            {
                row["sender_id"] = form.Id;
                lable_sender.Text = form.Name_s;
            }
        }

        private void bt_receiver_Click(object sender, EventArgs e)
        {
            Select_customer form = new Select_customer();

            if (form.ShowDialog() == DialogResult.OK)
            {
                row["receiver_id"] = form.Id;
                lable_receiver.Text = form.Name_s;
            }
        }

        private void bt_sendcity_Click(object sender, EventArgs e)
        {
            select_city form = new select_city();
            if (form.ShowDialog() == DialogResult.OK)
            {
                row["source_id"] = form.Id;
                lable_sendcity.Text = form.Name_s;
            }
        }

        private void bt_receivecity_Click(object sender, EventArgs e)
        {
            select_city form = new select_city();
            if (form.ShowDialog() == DialogResult.OK)
            {
                row["destin_id"] = form.Id;
                lable_reccity.Text = form.Name_s;
            }
        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            if (this.ValidateChildren())
            {
                try
                {
                    row["oper_id"] = session.Oper.id;
                    tborderBindingSource.EndEdit();
                    tb_orderTableAdapter.Update(this.dataSet1.tb_order);
                    foreach (DataRow r in dataSet1.tb_goods.Rows)
                    {
                        r["order_id"] = row["id"];
                    }
                    tb_goodsTableAdapter.Update(this.dataSet1.tb_goods);
                    this.Close();
                    MessageBox.Show("添加成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("出错啦" + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            
        }

        private void receive_date_ValueChanged(object sender, EventArgs e)
        {
            row["receive_date"] = receive_date.Value;
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            tborderBindingSource.CancelEdit();
            this.Close();          
            
        }

        private void lable_sender_Click(object sender, EventArgs e)
        {

        }

        private void goods_gridview_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            if(e.RowIndex % 2==0)
                goods_gridview.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.AliceBlue;
        }

        private void lable_sender_Validating(object sender, CancelEventArgs e)
        {
            if(lable_sender.Text.Equals(""))
            {
                error.SetError(lable_sender, "请选择");
                e.Cancel = true;
            }
            else
            {
                error.SetError(lable_sender, "");
            }
        }

        private void lable_receiver_Validating(object sender, CancelEventArgs e)
        {
            if (lable_receiver.Text.Equals(""))
            {
                error.SetError(lable_receiver, "请选择");
                e.Cancel = true;
            }
            else
            {
                error.SetError(lable_receiver, "");
            }
        }

        private void lable_sendcity_Validating(object sender, CancelEventArgs e)
        {
            if (lable_sendcity.Text.Equals(""))
            {
                error.SetError(lable_sendcity, "请选择");
                e.Cancel = true;
            }
            else
            {
                error.SetError(lable_sendcity, "");
            }
        }

        private void lable_reccity_Validating(object sender, CancelEventArgs e)
        {
            if (lable_reccity.Text.Equals(""))
            {
                error.SetError(lable_reccity, "请选择");
                e.Cancel = true;
            }
            else
            {
                error.SetError(lable_reccity, "");
            }
        }

        private void goods_gridview_Validating(object sender, CancelEventArgs e)
        {
            if (goods_gridview.RowCount > 0)
                error.SetError(goods_gridview, "");
            else
            {
                error.SetError(goods_gridview, "请填写订单物品");
                e.Cancel = true;
            }
        }

        private void goods_gridview_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            session.Color_Const.set_gv_color(goods_gridview);
        }
    }
}
